using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using NHibernate;

namespace OA.BLL
{
    public class Menu:Base
    {
        private DAL.Menu dal = null;
        public Menu()
        {
            isession = DB_help.NHibernateHelper.GetCurrentSession(DefaultDataBase);
            dal = new DAL.Menu(isession);
        }

        public Common.Result Add(Model.Menu value)
        {
            Common.Result ret = new Common.Result(false, 0, "修改失败");
            if (new WebSite().GetModel(value.WebSiteCode) == null)
            {
                ret.message = "[站点编号]已经存在";
                return ret;
            }
            if (dal.GetModel(value.WebSiteCode,value.Code) != null)
            {
                ret.message = "[资源编号]已经存在";
                return ret;

            }
            if (value.ParentCode != null)
            {
                value.Parent = dal.GetModel(value.WebSiteCode,value.ParentCode);
                if (value.Parent == null)
                {
                    ret.message = "[上级资源]不存在";
                    return ret;
                }
            }
            ret = dal.Add(value);
            return ret;
        }

        public Common.Result Update(Model.Menu value)
        {
            Common.Result ret = new Common.Result(false, 0, "修改失败");
            var dalvalue = dal.GetModel(value.WebSiteCode,value.Code);
            if (dalvalue == null)
            {
                ret.success = false;
                ret.count = 0;
                ret.message = "数据不存在,无法修改";
                return ret;
            }

            if (string.IsNullOrEmpty(value.ParentCode) == false)
            {
                dalvalue.Parent = dal.GetModel(value.WebSiteCode,value.ParentCode);
                if (dalvalue.Parent == null)
                {
                    ret.message = "上级菜单不存在";
                    return ret;
                }
                else
                {
                    var chk = dalvalue.Parent;
                    while (chk != null)
                    {
                        if (chk.Code == value.Code)
                        {
                            ret.message = "要修改的[上级菜单]为当前菜单的下级菜单，无法修改。";
                            return ret;
                        }
                        chk = chk.Parent;
                    }
                }
            }
            dalvalue.IconCls = value.IconCls;
            dalvalue.Name = value.Name;
            dalvalue.Address = value.Address;
            dalvalue.IsShow = value.IsShow;
            dalvalue.ParentCode = value.ParentCode;
            dalvalue.Sort = value.Sort;
            ret = dal.Update(dalvalue);
            return ret;
        }

        public Common.Result Delete(string webSiteCode, string menuCode)
        {
            var ret = dal.Delete(webSiteCode, menuCode);
            return ret;
        }

        public Model.Menu GetModel(string id)
        {
            return dal.GetModel(id);
        }

        public Model.Menu GetModel(string WebSiteCode, string MenuCode)
        {
            return dal.GetModel(WebSiteCode, MenuCode);
        }

        public IList<Model.Menu> GetByParent(string webSiteCode, string ParentCode,bool? IsShow)
        {
            return dal.GetByParent(webSiteCode, ParentCode,IsShow);
        }

        public IList<Model.Menu> Get(string where, string orderby, int start, int length,params object[] ps)
        {
            return dal.Get(where, orderby, start, length, ps);
        }

        public int GetCount(string where, params object[] ps)
        {
            return dal.GetCount(where, ps);
        }

    }
}
